package com.example.my.meila.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;

import com.example.my.meila.R;
import com.example.my.meila.api.ExceptionHandle;
import com.example.my.meila.constast.Constast;
import com.example.my.meila.entity.Global;
import com.example.my.meila.presenter.GlobalPresenter;
import com.example.my.meila.view.activity.GlobalWebViewActivity;
import com.example.my.meila.view.adapter.GlobalAdapter;
import com.example.my.meila.view.impl.GlobalView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class GlobalFragment extends Fragment implements GlobalView, GlobalAdapter.OnItemClickListener {
    private HeadViewHolder headViewHolder;
    @BindView(R.id.rv_global)
    XRecyclerView rv;
    private List<Global.DataBean.ModulesBean.ProductsBean> data;
    private List<Global.DataBean.ModulesBean> data2;
    private GlobalAdapter adapter;
    private GlobalPresenter presenter;

    public GlobalFragment() {
        // Required empty public constructor
    }

    public static GlobalFragment newInstance() {

        Bundle args = new Bundle();

        GlobalFragment fragment = new GlobalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_earth, container, false);
        ButterKnife.bind(this, view);
        presenter = new GlobalPresenter(this);
        presenter.getGlobalData();
        createData();
        rv.setArrowImageView(R.drawable.person_global_fresh_view);
        rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                rv.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                 rv.loadMoreComplete();
            }
        });
        return view;
    }

//    public class Refresh {
//        @BindView(R.id.iv_refresh)
//        ImageView ivRefresh;
//        View view;
//
//        public void Refresh() {
//
//            view = LayoutInflater.from(getContext()).inflate(R.layout.item_global_refresh_layout, null);
//            ButterKnife.bind(this, view);
//            AnimationDrawable anim = (AnimationDrawable) ivRefresh.getDrawable();
//            anim.stop();
//            anim.start();
//        }
//    }

    private void createData() {
        data = new ArrayList<>();
        data2 = new ArrayList<>();
        adapter = new GlobalAdapter(data, getContext());
        rv.setAdapter(adapter);
        LinearLayoutManager llm = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rv.setLayoutManager(llm);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onSuccess(Global global) {
        if (global != null) {
            data.addAll(global.getData().getModules().get(0).getProducts());
            data2.addAll(global.getData().getModules());

            headViewHolder = new HeadViewHolder();
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            headViewHolder.view.setLayoutParams(params);
            rv.addHeaderView(headViewHolder.view);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    @Override
    public void OnItemClickClickListener(int postion) {
        Intent intent = new Intent(getActivity(), GlobalWebViewActivity.class);
        if (postion == 0) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE1);
        }
        if (postion == 1) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE2);
        }
        if (postion == 2) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE3);
        }
        if (postion == 3) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE4);
        }
        if (postion == 4) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE5);
        }
        if (postion == 5) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE6);
        }
        if (postion == 6) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE7);
        }
        if (postion == 7) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE8);
        }
        if (postion == 8) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE9);
        }
        if (postion == 9) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE10);
        }
        if (postion == 10) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE11);
        }
        if (postion == 11) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE12);
        }
        if (postion == 12) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE13);
        }
        if (postion == 13) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE14);
        }
        if (postion == 14) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE15);
        }
        if (postion == 15) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE16);
        }
        if (postion == 16) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE17);
        }
        if (postion == 17) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE18);
        }
        if (postion == 18) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE19);
        }
        if (postion == 19) {
            intent.putExtra("news", Constast.GLOBAL_WV_PAGE20);
        }

        intent.putExtra("name", data.get(postion).getName());
        startActivity(intent);
    }

    public class HeadViewHolder {
        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv2)
        TextView tv2;
        View view;

        public HeadViewHolder() {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_global_head_layout, null);
            ButterKnife.bind(this, view);

            tv1.setText(data2.get(0).getTitle());
            tv2.setText(data2.get(0).getIntro());
        }


    }
}

